package com.myacg.editor.common.unit.panel.base;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.myacg.editor.common.unit.button.TextButton;
import com.myacg.editor.common.utils.JFrameUtils;
/**
 * 面板对应的拖拽区域和基本的按钮;
 * @author Administrator
 *
 */
public abstract class DefaultPanelBar extends JPanel{
	private JPanel centerPanel ; 
	private JPanel rightPanel ; 
	private Color backgroundColor ;
	private Color borderBgColor;
	private TextButton closeButton ; 
	private TextButton hideButton ; 
	private String barName;
	private MouseAdapter mouseAdapter;
	public DefaultPanelBar(String barName,int height){
		this.setLayout(new BorderLayout());
		this.barName = barName;
		centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 1));
		centerPanel.setOpaque(false);
		rightPanel.setOpaque(false);
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(rightPanel, BorderLayout.EAST);
		this.setPreferredSize(new Dimension(getWidth(), height));
		this.backgroundColor =new Color(230 , 230, 230 );
		this.borderBgColor = new Color(200 , 200, 200 );
		this.setBackground(this.backgroundColor);
		//初始化按钮和按钮的事件
		initBar(height);
		dragBarEvent();
		this.repaint();
	}
	private void initBar(int height){
		//设置一个默认的关闭按钮
		closeButton = new TextButton(height, height-1, "X" , new Color(50 , 50, 50 ) , backgroundColor);
		hideButton  = new TextButton(height, height-1, "-", new Color(50 , 50, 50 ) , backgroundColor);
		closeButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				//延后关闭事件, 传入是本组件的父组件 
				closeEvent();
			}
		});
		hideButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				//延后隐藏事件, 传入是本组件的父组件 
				hideEvent();
			}
		});
		rightPanel.add(hideButton);
		rightPanel.add(closeButton);
		centerPanel.add(new JLabel(barName));
		 
	}
	/**
	 * 拖拽动态设置父组件位置的事件
	 */
	private void dragBarEvent(){
	   mouseAdapter = new MouseAdapter() {
			private int x=0;
			private int y=0;
			private int selfx=0;
			private int selfy=0;
			//拖拽
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseDragged(e);
				//判断是否越界
				if(getParent().getParent().getBounds().contains(getParent().getX()+e.getX(),getParent().getY()+e.getY())){
					//未越界则重新设定距离
					getParent().setBounds(selfx-(x-e.getXOnScreen()), selfy-(y-e.getYOnScreen()), getParent().getWidth(), getParent().getHeight());
				}
			}
			//按下
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mousePressed(e);
				x=e.getXOnScreen();
				y=e.getYOnScreen();
				//组件的位置
				selfx = getParent().getX();
				selfy = getParent().getY();
				setCursor(new Cursor(Cursor.MOVE_CURSOR));
			}
			//松开
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseReleased(e);
				x=e.getXOnScreen();
				y=e.getYOnScreen();
				//组件的位置
				selfx =	getParent().getX();
				selfy =	getParent().getY();
				setCursor( Cursor.getDefaultCursor());
			}
		};
		this.addMouseListener(mouseAdapter);
		this.addMouseMotionListener(mouseAdapter);
	}
	/**
	 * 推迟事件的执行
	 * @param parent
	 */
	public  abstract  void   closeEvent();
	/**
	 * 推迟事件的执行
	 * @param parent
	 */
	public  abstract  void   hideEvent();
	
	/**
	 * 对面板进行简单的绘制
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(this.backgroundColor);
 		g.fillRect(0,0, getWidth(), getHeight());
 		g.setColor(this.borderBgColor);
 		g.drawLine(0, getHeight()-1, getWidth()*2, getHeight());
	}
	
	
	public String getBarName() {
		return barName;
	}
	public void setBarName(String barName) {
		this.barName = barName;
	}
	
	
	
}
